<?php
class CategoryDao{
	private $con = null;

	public function getCategories($parentId, $level){
		$result = DbUtils::execute("SELECT * FROM category WHERE parentId = ".$parentId." AND level=".$level);
		
		$categories = array();
		while($row = mysql_fetch_array($result)){
			$category = new Category();
			$category->construct($row);
			array_push($categories, $category);
		}
				
		return $categories;
	}

	public function updateCategory($category){
		$level = $category->level;
		$parentId = $category->parentId;
		$order = $category->order;	
		$name = $category->name;
		$pageId = $category->pageId;
		$id = $category->id;
		$visible = $category->visible;   
		
		$result = DbUtils::execute("SELECT count(*) as num FROM category WHERE id=".$id);
		$r = mysql_fetch_array($result);
		if($id != null && $r["num"]>0){
			DbUtils::execute("UPDATE `category` SET `name` = '".$name."', `level` = '".$level."', `parentId` = '".$parentId."', `order` = '".$order."', `pageId` ='".$pageId."', `visible` = ".$visible." WHERE `id` = ".$id);
		}else{
			DbUtils::execute("INSERT INTO category (`id`, `name`, `level`, `parentId`, `order`, `pageId`,`visible`) VALUES "
			."('', '".$name."', '".$level."','".$parentId."','".$order."','".$pageId."',".$visible.")");
		}
	}
	
	public function deleteCategory($id){
		DbUtils::execute("DELETE FROM `asiatec`.`category` WHERE `category`.`id` = ".$id);
	}
}


class Category{
	public $id;
	public $name;
	public $level;
	public $parentId;
	public $order;
	public $pageId;
	public $visible;
	
	public function construct($row){
		$this->id = $row["id"];
		$this->name = $row["name"];
		$this->level = $row["level"];
		$this->parentId = $row["parentId"];
		$this->order = $row["order"];
		$this->pageId = $row["pageId"];
		$this->visible = $row["visible"];
	}
}
?>



